#include "const/oPreDefine.h"
#include "physics/oGroup.h"
#include "event/oEvent.h"

oGroup* oGroup::groups[16];
oGroup oGroup::_zero(0);
oGroup oGroup::_one(1);
oGroup oGroup::_two(2);
oGroup oGroup::_three(3);
oGroup oGroup::_four(4);
oGroup oGroup::_five(5);
oGroup oGroup::_six(6);
oGroup oGroup::_seven(7);
oGroup oGroup::_eight(8);
oGroup oGroup::_nine(9);
oGroup oGroup::_ten(10);
oGroup oGroup::_eleven(11);
oGroup oGroup::_twelve(12);
oGroup oGroup::_thirteen(13);
oGroup oGroup::_fourteen(14);
oGroup oGroup::_fifteen(15);

oGroup* oGroup::zero(&_zero);
oGroup* oGroup::one(&_one);
oGroup* oGroup::two(&_two);
oGroup* oGroup::three(&_three);
oGroup* oGroup::four(&_four);
oGroup* oGroup::five(&_five);
oGroup* oGroup::six(&_six);
oGroup* oGroup::seven(&_seven);
oGroup* oGroup::eight(&_eight);
oGroup* oGroup::nine(&_nine);
oGroup* oGroup::ten(&_ten);
oGroup* oGroup::eleven(&_eleven);
oGroup* oGroup::twelve(&_twelve);
oGroup* oGroup::thirteen(&_thirteen);
oGroup* oGroup::fourteen(&_fourteen);
oGroup* oGroup::fifteen(&_fifteen);

oGroup::oGroup( uint16 index ):
b2Filter(1<<index, 0, index),
_userData(nullptr)
{
	groups[index] = this;
}

void oGroup::setUserData( void* data )
{
	_userData = data;
}

void* oGroup::getUserData()
{
	return _userData;
}

void oGroup::setShouldContact( oGroup* groupA, oGroup* groupB, bool contact )
{
	if (contact)
	{
		groupA->_maskBits |= groupB->_categoryBits;
		groupB->_maskBits |= groupA->_categoryBits;
	}
	else
	{
		groupA->_maskBits &= (~groupB->_categoryBits);
		groupB->_maskBits &= (~groupA->_categoryBits);
	}
	oPureEvent::send(EVENT_REFILTER);
}

void oGroup::setShouldContact( uint16 indexA, uint16 indexB, bool contact )
{
	oGroup* groupA = groups[indexA];
	oGroup* groupB = groups[indexB];
	oGroup::setShouldContact(groupA, groupB, contact);
}

oGroup* oGroup::get( uint16 index )
{
	return groups[index];
}

bool oGroup::getShouldContact( oGroup* groupA, oGroup* groupB )
{
	return (groupA->getMaskBits() & groupB->getCategoryBits()) != 0 && (groupA->getCategoryBits() & groupB->getMaskBits()) != 0;
}

bool oGroup::getShouldContact( uint16 indexA, uint16 indexB )
{
	oGroup* groupA = groups[indexA];
	oGroup* groupB = groups[indexB];
	return (groupA->getMaskBits() & groupB->getCategoryBits()) != 0 && (groupA->getCategoryBits() & groupB->getMaskBits()) != 0;
}

const string oGroup::EVENT_REFILTER = "oRefilter";
